package site.fsyj.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import site.fsyj.bean.AutoMihayou;
import site.fsyj.util.CookieUtil;
import site.fsyj.util.ResourceUtil;

import java.util.Map;
import java.util.Properties;

/**
 * @author fsyj on 2022/3/7
 */
@Slf4j
@Component
@Configuration
public class AppConfig {

    private static final Properties properties = ResourceUtil.getConfigure("appConfig.properties");

    @Bean
    public AutoMihayou autoMihayou() {
        if (properties == null) {
            log.error("配置文件读取错误，系统结束");
            System.exit(-1);
        }
        AutoMihayou mihayou = new AutoMihayou();
        String cookie = properties.getProperty("cookie");
        mihayou.setCookie(cookie);
        // 将cookie转为map
        Map<String, String> map = CookieUtil.cookieToMap(cookie);
        mihayou.setSuid(map.get("account_id"));
        mihayou.setToken(map.get("cookie_token"));
        mihayou.setName("自动任务");
        return mihayou;
    }
}
